package com.aebiz.usercenter.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.apache.bcel.classfile.ConstantObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aebiz.usercenter.common.controller.IBaseController;
import com.aebiz.usercenter.common.util.ConstatFinalUtil;
import com.aebiz.usercenter.common.util.PoiUtil;
import com.aebiz.usercenter.goods.pojo.AGoodsBrand;
import com.aebiz.usercenter.goods.pojo.AGoodsCate;
import com.aebiz.usercenter.goods.pojo.AGoodsCateEnum;
import com.aebiz.usercenter.goods.service.IGoodsService;
import com.aebiz.usercenter.goods.service.IStatService;
import com.aebiz.usercenter.users.service.IUsersService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/stat")
public class HighchartsCotroller extends IBaseController{
	
	@Resource
	private IGoodsService goodsService;
	
	@Resource
	private IStatService statService;
	
	@Resource
	private PoiUtil poiUtil;
	/**
	 * 商品列表(统计)
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/statsList")
	public String goodsList(String st,String ed,Model model,HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==statsList==");
		/* 取数据 */
		this.goodsListJSON(request,st, ed, model);
		
		/*
		 * 区分,何时是访问数据,
		 * 何时是下载数据
		 *  */
    	String operType = request.getParameter("operType");
		if("downFile".equalsIgnoreCase(operType))
		{
			 /*获取数据,下载 */
			List<Map> statList = (List<Map>) request.getAttribute("statList");
			/* 拼装所需要的数据List<List<String>> */
			List<List<String>> dataList = new ArrayList<List<String>>();
			
			List<String> titleList = new ArrayList<String>();
			titleList.add("添加时间");
			titleList.add("数量");
			dataList.add(titleList);
			
			 /*放数据*/ 
			for (Iterator iterator = statList.iterator(); iterator.hasNext();)
			{
				Map map = (Map) iterator.next();
				
				/* 每一行的数据*/ 
				List<String> rowList = new ArrayList<String>();
				rowList.add(map.get("createTime") + "");
				rowList.add(map.get("count") + "");
				dataList.add(rowList);
			}
			
			/* List<List<String>>拼装完成
			 * os:给response,让服务器弹出下载框
			 *  
			 告诉浏览器,我要下载 */
			response.reset();
			 /*响应头:
			 * MIME类型,告诉浏览器,不管啥文件,都弹出下载框 */
			response.setHeader("Content-disposition","attachment;filename=1.xls");
			this.poiUtil.writeFile(dataList, response.getOutputStream());
			return null ;
		}
		return "/back/admins/goodsList" ; 
	}
 
	/**
	 * easyui 要求,必须提供一个接口,用来获取数据
	 * @param st
	 * @param ed
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goodsListJSON",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String goodsListJSON(HttpServletRequest request,String st,String ed,Model model)
	{
		JSONObject resultJSON = new JSONObject() ; 
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = ""; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.strDateTime(st);
			edDate = this.dateFormatUtil.strDateTime(ed);
		}
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 需要两个时间 */
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		List<Map> statList = this.statService.statGoodsMapService(condMap);
		/* 存储结果 */
		model.addAttribute("statList", statList);
		request.setAttribute("statList", statList);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		
		resultJSON.put("total", statList.size());
		
		JSONArray rowsArr = new JSONArray() ; 
		/* 拼装json */
		for (Iterator iterator = statList.iterator(); iterator.hasNext();)
		{
			Map map = (Map) iterator.next();
			/* 数据项 */
			JSONObject rowJSON = new JSONObject() ; 
			rowJSON.put("createTime", map.get("createTime"));
			rowJSON.put("count", map.get("count"));
			
			rowsArr.add(rowJSON);
		}
		resultJSON.put("rows", rowsArr);
		return resultJSON.toJSONString(); 
	}
	
	/**
	 * 添加，主要是为了显示树形结构
	 * @param model
	 * @return
	 */
	@RequestMapping("/goodsInsert")
	public String goodsInsert(Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=========goodsInsert=========");
		Map<String, Object> condMap=new HashMap<>();
		
		/*查询所有品牌*/
		condMap.clear();
		condMap.put("status", AGoodsCateEnum.STATUS_ENABLE.getStatus());
		Map<String, Object> resultMap=this.goodsService.findCondListGoodsBrandService(null, condMap);
		List<AGoodsBrand> brandList=(List<AGoodsBrand>) resultMap.get("list");
		/* 放到四大作用域中 */
		model.addAttribute("brandList", brandList);
		
		/* 查询分类 */
		List<AGoodsCate> goodsCateTreeList = this.goodsService.findTreeListCateService();
		/* 放到四大作用域中 */
		model.addAttribute("goodsCateTreeList", goodsCateTreeList);
		return "/back/admins/goodsInsert";
	}
}
